package com.loong.web.gp4j.view.manage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.loong.web.gp4j.bean.schema.user.Auths;
import com.loong.web.gp4j.service.error.ErrorService;
import com.loong.web.suppert.collection.param.PageParam;
import com.loong.web.suppert.collection.vo.ResultVo;
import com.loong.web.suppert.collection.vo.ResultVoFactory;
import com.loong.web.suppert.intercept.auth.AuthType;

/**
 * 异常日志资源
 *
 * @author 张成轩
 */
@Controller
@RequestMapping("manage/errors")
public class ErrorsViewMapping {

	@Resource
	private ErrorService errorService;

	/**
	 * 异常信息页面
	 * 
	 * @param request 请求
	 * @param type 异常类型
	 * @param page 分页
	 * @return 视图
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	@AuthType(Auths.SYS_MANAGER)
	public ModelAndView errors(HttpServletRequest request, String type, PageParam page) {

		ModelAndView model = new ModelAndView("gp4j/manage/errors");
		model.addObject("types", errorService.getTypes());
		if (type != null)
			model.addObject("type_detail", errorService.queryErrors(type, page));
		return model;
	}

	/**
	 * 移除异常类型
	 * 
	 * @param type 异常类型
	 * @return 返回对象
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST)
	@ResponseBody
	@AuthType(Auths.SYS_MANAGER)
	public ResultVo<?> remove(String type) {

		if (type == null)
			return ResultVoFactory.fail("异常类型不能为空");
		return errorService.remove(type);
	}
}
